import type {
  CreateCrudOptionsProps,
  CreateCrudOptionsRet,
  CrudExpose,
} from '@fast-crud/fast-crud';

import type { FormRulesExt } from '#/api';
import type { DefGenTestSimpleModel } from '#/api/devOperation/developer/model/defGenTestSimpleModel';

import { ref } from 'vue';
import { useRouter } from 'vue-router';

import { $t } from '@vben/locales';

import { DefGenTestSimpleApi } from '#/api/devOperation/developer/defGenTestSimple';
import { ActionEnum, DictEnum } from '#/enums/commonEnum';
import {
  backendDict,
  createdTimeColumn,
  deleteButton,
  indexColumn,
  YES_NO_CONSTANT_DICT,
} from '#/plugins/fast-crud/common';

type ContextRef = object;

export function createCrudOptions(
  props: CreateCrudOptionsProps<
    DefGenTestSimpleModel.DefGenTestSimpleResultVO,
    ContextRef
  >,
): CreateCrudOptionsRet<DefGenTestSimpleModel.DefGenTestSimpleResultVO> {
  const selectedRowKeys = ref<string[]>([]);
  const { push } = useRouter();
  // 注意，menuName 要跟(/anyone/visible/resource)实际返回的路由name一致才行。
  const menuName = '测试树结构维护编辑';
  return {
    crudOptions: {
      request: {
        pageRequest: DefGenTestSimpleApi.pageRequest,
        addRequest: DefGenTestSimpleApi.addRequest,
        editRequest: DefGenTestSimpleApi.editRequest,
        delRequest: DefGenTestSimpleApi.delRequest,
        infoRequest: DefGenTestSimpleApi.infoRequest,
      },
      actionbar: {
        buttons: {
          add: {
            click() {
              push({
                name: menuName,
                params: { type: ActionEnum.ADD, id: '0' },
              });
            },
          },
          ...deleteButton({
            crudExpose: props.crudExpose,
            selectedRowKeys,
            removeFn: DefGenTestSimpleApi.remove,
          }),
        },
      },
      form: {
        wrapper: {},
      },
      table: {
        striped: true,
        rowKey: 'id',
        rowSelection: {
          type: 'checkbox',
          selectedRowKeys,
          onChange: (changed: string[]) => {
            selectedRowKeys.value = changed;
          },
        },
      },
      rowHandle: {
        width: '200px',
        buttons: {
          edit: {
            click({ row }) {
              push({
                name: menuName,
                params: { type: ActionEnum.EDIT, id: row.id },
              });
            },
          },
          view: {
            show: true,
            click({ row }) {
              push({
                name: menuName,
                params: { type: ActionEnum.VIEW, id: row.id },
              });
            },
          },
          remove: {},
          copy: {},
        },
      },
      columns: {
        ...indexColumn(props.crudExpose),
        name: {
          title: $t('devOperation.developer.defGenTestSimple.name'),
          type: 'text',
          search: { show: true },
          addForm: {
            show: true,
            value: '大法师',
          },
          editForm: { show: true },
          form: {
            component: {},
          },
          column: {
            show: true,
          },
        },
        parentId: {
          title: $t('devOperation.developer.defGenTestSimple.parentId'),
          type: 'text',
          search: { show: true },
          addForm: {
            show: true,
          },
          editForm: { show: true },
          form: {
            component: {},
          },
          column: {
            show: true,
          },
        },
        sortValue: {
          title: $t('devOperation.developer.defGenTestSimple.sortValue'),
          type: 'text',
          search: { show: true },
          addForm: {
            show: true,
            value: '22',
          },
          editForm: { show: true },
          form: {
            component: {},
          },
          column: {
            show: true,
          },
        },
        stock: {
          title: $t('devOperation.developer.defGenTestSimple.stock'),
          type: 'text',
          search: { show: true },
          addForm: {
            show: true,
          },
          editForm: { show: true },
          form: {
            component: {},
          },
          column: {
            show: true,
          },
        },
        type: {
          title: $t('devOperation.developer.defGenTestSimple.type'),
          type: 'dict-radio',
          dict: backendDict({ type: 'ProductType', isEnum: true }),
          search: { show: true },
          addForm: {
            show: true,
          },
          editForm: { show: true },
          form: {
            component: {
              optionName: 'a-radio-button',
            },
          },
          column: {
            show: true,
            formatter: (scope) => {
              return scope.row?.echoMap?.type;
            },
          },
        },
        type2: {
          title: $t('devOperation.developer.defGenTestSimple.type2'),
          type: 'dict-radio',
          dict: backendDict({
            type: 'DefGenTestSimpleType2Enum',
            isEnum: true,
          }),
          search: { show: true },
          addForm: {
            show: true,
          },
          editForm: { show: true },
          form: {
            component: {
              optionName: 'a-radio-button',
            },
          },
          column: {
            show: true,
            formatter: (scope) => {
              return scope.row?.echoMap?.type2;
            },
          },
        },
        type3: {
          title: $t('devOperation.developer.defGenTestSimple.type3'),
          type: 'dict-radio',
          // 建议将魔法数参数移动到 DictEnum 中，并添加为: EchoDictType_Global_EDUCATION = 'EDUCATION';
          // 'EDUCATION' 需要与后端DictType类中的参数 以及 def_dict表中的key字段 保持一致，否则无法回显！
          // dict: backendDict(DictEnum.EchoDictType_Global_EDUCATION),
          dict: backendDict('EDUCATION'),
          search: { show: true },
          addForm: {
            show: true,
          },
          editForm: { show: true },
          form: {
            component: {
              optionName: 'a-radio-button',
            },
          },
          column: {
            show: true,
            formatter: (scope) => {
              return scope.row?.echoMap?.type3;
            },
          },
        },
        state: {
          title: $t('devOperation.developer.defGenTestSimple.state'),
          type: 'dict-radio',
          dict: YES_NO_CONSTANT_DICT,
          search: { show: true },
          addForm: {
            show: true,
            value: false,
          },
          editForm: { show: true },
          form: {
            component: {
              optionName: 'a-radio-button',
            },
          },
          column: {
            show: true,
          },
        },
        test4: {
          title: $t('devOperation.developer.defGenTestSimple.test4'),
          type: 'text',
          search: { show: true },
          addForm: {
            show: true,
          },
          editForm: { show: true },
          form: {
            component: {},
          },
          column: {
            show: true,
          },
        },
        test5: {
          title: $t('devOperation.developer.defGenTestSimple.test5'),
          type: 'date',
          search: { show: true },
          addForm: {
            show: true,
          },
          editForm: { show: true },
          form: {
            component: {
              format: 'YYYY-MM-DD',
              valueFormat: 'YYYY-MM-DD',
            },
          },
          valueBuilder({ value, row, key }) {
            if (value !== null && row) {
              row[key] = value;
            }
          },
          column: {
            show: true,
          },
        },
        test6: {
          title: $t('devOperation.developer.defGenTestSimple.test6'),
          type: 'datetime',
          search: { show: true },
          addForm: {
            show: true,
          },
          editForm: { show: true },
          form: {
            component: {
              defaultTime: '00:00:00',
              format: 'YYYY-MM-DD HH:mm:ss',
              valueFormat: 'YYYY-MM-DD HH:mm:ss',
            },
          },
          valueBuilder({ value, row, key }) {
            if (value !== null && row) {
              row[key] = value;
            }
          },
          column: {
            show: true,
          },
        },
        label: {
          title: $t('devOperation.developer.defGenTestSimple.label'),
          type: 'text',
          search: { show: true },
          addForm: {
            show: true,
          },
          editForm: { show: true },
          form: {
            component: {},
          },
          column: {
            show: true,
          },
        },
        test7: {
          title: $t('devOperation.developer.defGenTestSimple.test7'),
          type: 'dict-radio',
          dict: backendDict(DictEnum.Sex),
          search: { show: true },
          addForm: {
            show: true,
            value: '01',
          },
          editForm: { show: true },
          form: {
            component: {
              optionName: 'a-radio-button',
            },
          },
          column: {
            show: true,
            formatter: (scope) => {
              return scope.row?.echoMap?.test7;
            },
          },
        },
        test12: {
          title: $t('devOperation.developer.defGenTestSimple.test12'),
          type: 'dict-radio',
          // 建议将魔法数参数移动到 DictEnum 中，并添加为: EchoDictType_Global_TEST_ADD_DICT = 'TEST_ADD_DICT';
          // 'TEST_ADD_DICT' 需要与后端DictType类中的参数 以及 def_dict表中的key字段 保持一致，否则无法回显！
          // dict: backendDict(DictEnum.EchoDictType_Global_TEST_ADD_DICT),
          dict: backendDict('TEST_ADD_DICT'),
          search: { show: true },
          addForm: {
            show: true,
          },
          editForm: { show: true },
          form: {
            component: {
              optionName: 'a-radio-button',
            },
          },
          column: {
            show: true,
            formatter: (scope) => {
              return scope.row?.echoMap?.test12;
            },
          },
        },
        userId: {
          title: $t('devOperation.developer.defGenTestSimple.userId'),
          type: 'dict-radio',
          search: { show: true },
          addForm: {
            show: true,
          },
          editForm: { show: true },
          form: {
            component: {
              optionName: 'a-radio-button',
            },
          },
          column: {
            show: true,
            formatter: (scope) => {
              return scope.row?.echoMap?.userId;
            },
          },
        },
        orgId: {
          title: $t('devOperation.developer.defGenTestSimple.orgId'),
          type: 'dict-radio',
          search: { show: true },
          addForm: {
            show: true,
          },
          editForm: { show: true },
          form: {
            component: {
              optionName: 'a-radio-button',
            },
          },
          column: {
            show: true,
            formatter: (scope) => {
              return scope.row?.echoMap?.orgId;
            },
          },
        },
        test8: {
          title: $t('devOperation.developer.defGenTestSimple.test8'),
          type: 'number',
          search: { show: true },
          addForm: {
            show: true,
            value: 21.23,
          },
          editForm: { show: true },
          form: {
            component: {},
          },
          column: {
            show: true,
          },
        },
        test9: {
          title: $t('devOperation.developer.defGenTestSimple.test9'),
          type: 'text',
          search: { show: true },
          addForm: {
            show: true,
          },
          editForm: { show: true },
          form: {
            component: {},
          },
          column: {
            show: true,
          },
        },
        test10: {
          title: $t('devOperation.developer.defGenTestSimple.test10'),
          type: 'number',
          search: { show: true },
          addForm: {
            show: true,
          },
          editForm: { show: true },
          form: {
            component: {},
          },
          column: {
            show: true,
          },
        },
        test11: {
          title: $t('devOperation.developer.defGenTestSimple.test11'),
          type: 'number',
          search: { show: true },
          addForm: {
            show: true,
          },
          editForm: { show: true },
          form: {
            component: {},
          },
          column: {
            show: true,
          },
        },
        ...createdTimeColumn({}),
      },
    },
  };
}

export const frontRules = (
  _crudExpose: CrudExpose,
  _mode: ActionEnum,
): FormRulesExt => {
  return {};
};
